package shuashuami.hb.com.adapter;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONObject;

import java.util.List;

import shuashuami.hb.com.entity.agent.ApplyMsgBean;
import shuashuami.hb.com.entity.agent.ClickerBean;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.glid.GlidUtil;
import shuashuami.hb.com.http.HttpAgentMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.view.ClickViewApplyDialog;

/**
 * <b>类   名：</b>${TYPE_NAME} <br/>
 * <b>类描述：</b> <br/>
 * <b>创建人：</b>zhangqi<br/>
 * <b>创建时间：</b>2017/6/24<br/>
 * <b>修改人：</b>zhangqi<br/>
 * <b>修改时间：</b>2017/6/24<br/>
 * <b>修改备注：</b><br/>
 *
 * @version 1.0.0<br/>
 */
public class AClickManagerAdapter2 extends BaseAdapter {

    private List<ClickerBean> mResource;
    private Context mContext;
    private LayoutInflater mInflater;
    private GlidUtil glidUtil;


    public AClickManagerAdapter2(Context context, List<ClickerBean> lists) {
        this.mContext = context;
        this.mResource = lists;
        mInflater = LayoutInflater.from(context);
        glidUtil = new GlidUtil(context);
    }

    @Override
    public int getCount() {
        return mResource.size();
    }

    @Override
    public Object getItem(int position) {
        return mResource.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_click_manager2, null);
            holder.imghead = (ImageView) convertView.findViewById(R.id.item_click_imghead);
            holder.txtname = (TextView) convertView.findViewById(R.id.item_click_txtname);
            holder.txtBtn = (TextView) convertView.findViewById(R.id.item_click_txtstatue);
            holder.txtmsg = (TextView) convertView.findViewById(R.id.item_click_txtmsg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final ClickerBean info = mResource.get(position);
        glidUtil.uploadCircle(info.getAvatar(), holder.imghead, R.mipmap.ic_launcher);
        holder.txtname.setText(info.getUsername());

        if (info.getStatus() == 0) {
            holder.txtBtn.setText("已拒绝");
            holder.txtBtn.setTextColor(Color.RED);
            holder.txtBtn.setBackgroundColor(0xffffff);
        } else if (info.getStatus() == 1) {
            holder.txtBtn.setText("查看申请");
            holder.txtBtn.setTextColor(Color.WHITE);
            holder.txtBtn.setBackgroundResource(R.drawable.shape_corner_main);

            holder.txtBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDialog(info);
                }
            });
        } else if (info.getStatus() == 2) {
//            holder.txtBtn.setText("查看申请");
//            holder.txtBtn.setTextColor(Color.WHITE);
//            holder.txtBtn.setBackgroundResource(R.drawable.shape_corner_main);
//
//            holder.txtBtn.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Log.i("zq", "====哈哈哈哈我点击了");
//                    showDialog(info);
//                }
//            });
        }

        holder.txtmsg.setText("ID：" + info.getId() + " " + info.getAddtime());


        return convertView;
    }

    private void showDialog(final ClickerBean info) {


//        // 访问网络，查看url
        HttpOnNextListener<String> httpOnNextListener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                resolveByJson(s, info);


            }
        };
//
//
        HttpAgentMethods.getApplyInfo(httpOnNextListener,
                info.getId()
        );


    }

    public void resolveByJson(String str, ClickerBean info) {
        try {
            JSONObject object = new JSONObject(str);

            int status = object.getInt("status");
            if (status == 200) {
                JSONObject content = object.getJSONObject("content");

                ApplyMsgBean bean = new ApplyMsgBean();
                bean.resoloveByJson(content);
                info.setMsgBean(bean);
                Message msg = Message.obtain();
                msg.what = 0x1;
                msg.obj = info;
                mHandler.sendMessage(msg);
            } else {
                String message = object.getString("message");
                Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {

        }

    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            int what = msg.what;
            if (what == 0x1) {
                ClickerBean bean = (ClickerBean) msg.obj;
                ClickViewApplyDialog changeContentDialog = new ClickViewApplyDialog(mContext);
                changeContentDialog.setContent(bean);
                changeContentDialog.show();
            }
        }
    };

//    public void getViewApplyURL(){
//
//    }

    class ViewHolder {
        ImageView imghead;
        TextView txtname;
        TextView txtmsg;
        TextView txtBtn;
    }
}
